package com.trkj.service.wxy;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.wxy.DefinitionDeptDao_wxy;
import com.trkj.dao.wxy.QuestionBankDao;
import com.trkj.pojo.DefinitionDept;
import com.trkj.pojo.QuestionBank;
import com.trkj.service.ServiceException;
import com.trkj.vo.wxy.QuestionBankPageVo;



@Service
@Transactional(rollbackFor = Exception.class)
public class QuestionBankService {
	@Autowired
	private QuestionBankDao questionBankDao;
	@Autowired
	private DefinitionDeptDao_wxy definitionDeptDao;
	
	/**
	 * 查询所有的记录
	 */
	public List<QuestionBankPageVo> queryByQuestionPage(int page,int rows) throws ServiceException{
		try {
			Sort sort = new Sort(Direction.DESC,"quDate");
			List<Object[]> list = questionBankDao.queryByQuestion(new PageRequest(page-1, rows,sort));
			
			List<QuestionBankPageVo> listQuestionPageVo = new ArrayList<>();
			if(list != null){
				for (Object[] objs : list) {
					QuestionBank q = (QuestionBank) objs[0];
					DefinitionDept d = (DefinitionDept) objs[1];
					
					QuestionBankPageVo questionBankPageVo = new QuestionBankPageVo();
					questionBankPageVo.setQuId(q.getQuId());
					questionBankPageVo.setQuName(q.getQuName());
					questionBankPageVo.setDefideName(d.getDefideName());
					questionBankPageVo.setNumberRadio(q.getNumberRadio());
					questionBankPageVo.setNumberMultiple(q.getNumberMultiple());
					questionBankPageVo.setNumberJudge(q.getNumberJudge());
					questionBankPageVo.setCompositeNumber(q.getCompositeNumber());
					questionBankPageVo.setQuRemark(q.getQuRemark());
					questionBankPageVo.setQuBlockup(q.getQuBlockup());
					questionBankPageVo.setQuPerson(q.getQuPerson());
					questionBankPageVo.setQuDate(q.getQuDate());
					
					listQuestionPageVo.add(questionBankPageVo);
				}
			}
			return listQuestionPageVo;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 查询所有的记录题库
	 */
	public List<QuestionBank> findAll_quName(){
		return (List<QuestionBank>) questionBankDao.findAll();
	}
	
	/**
	 * 查询所有的记录部门
	 */
	public List<DefinitionDept> findAll_defideName(){
		return (List<DefinitionDept>) definitionDeptDao.findAll();
	}
	
	
	/**
	 * 获取总记录数
	 * @return
	 */
	public long empByPageCount(){
		return questionBankDao.count();
		
	}
	
	/**
	 * 分页查询 
	 * @param page
	 * @param rows
	 * @return
	 */
	public List<QuestionBank> queryByPage(int page, int rows){
		return questionBankDao.queryByPage(new PageRequest(page - 1, rows));
	}
	
	
	/**
	 * 根据考试的需求进行查询 
	 * @param eId
	 * @param eIdstartDate
	 * @param eIdendTime
	 * @return
	 */
	public List<QuestionBank> queryByName(String quName){
		return questionBankDao.queryByeName("%"+quName+"%");
	}
	
	/**
	 * 新增
	 * @return
	 * @throws ServiceException 
	 */
	public void questionBank_insert(String quName,String defideName,String quRemark,String quBlockup) throws ServiceException{
		try {
			QuestionBank questionBank = new QuestionBank();
			DefinitionDept definitionDept = definitionDeptDao.queryByDefinitionDeptName(defideName);
			questionBank.setQuName(quName);			
			questionBank.setQuRemark(quRemark);
			questionBank.setQuBlockup(quBlockup);
			questionBank.setNumberMultiple(new BigDecimal(0));
			questionBank.setNumberJudge(new BigDecimal(0));
			questionBank.setNumberRadio(new BigDecimal(0));
			questionBank.setCompositeNumber(new BigDecimal(0));
			questionBank.setDefinitionDept(definitionDept);
			questionBank.setQuDate(new Timestamp(new Date().getTime()));
			questionBankDao.save(questionBank);
			 
		
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
			
		}
	}
	
	
	/**
	 * 删除
	 * @param upId
	 * @throws ServiceException
	 */
	public void QuestionBank_delete(Long quId) throws ServiceException{
		try {
			questionBankDao.delete(quId);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
	}
}
